using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(PlayerInput))]
public class Player : MonoBehaviour
{
    public Rigidbody Rigidbody { get; private set; }
    public Transform MainCameraTransform { get; private set; }

    [SerializeField]
    private PlayerSO data;

    public PlayerSO Data => data;

    [SerializeField]
    private PlayerCapsuleColliderUtility colliderUtility;

    public PlayerCapsuleColliderUtility ColliderUtility => colliderUtility;

    [SerializeField]
    private PlayerLayerData playerLayerData;

    public PlayerLayerData PlayerLayerData => playerLayerData;

    public PlayerInput PlayerInput { get; private set; }

    [SerializeField]
    private PlayerAnimationData animationData;

    public Animator Animator { get; private set; }

    public PlayerAnimationData AnimationData
    {
        get => animationData;
        private set => animationData = value;
    }

    private PlayerMovementMachine movementMachine;

    private void Awake()
    {
        Rigidbody = GetComponent<Rigidbody>();
        MainCameraTransform = Camera.main.transform;
        PlayerInput = GetComponent<PlayerInput>();
        movementMachine = new PlayerMovementMachine(this);
        Animator = GetComponentInChildren<Animator>();

        animationData.Initialize();
        ColliderUtility.Initialize(gameObject);
        ColliderUtility.CalculateCapsuleColliderDismensions();
    }

    private void Start()
    {
        movementMachine.ChangeState(movementMachine.IdlingState);
    }

    private void OnValidate()
    {
        ColliderUtility.Initialize(gameObject);
        ColliderUtility.CalculateCapsuleColliderDismensions();
    }

    private void Update()
    {
        movementMachine.InputHandle();
        movementMachine.Update();
    }

    private void FixedUpdate()
    {
        movementMachine.PhysicsUpdate();
    }

    private void OnTriggerEnter(Collider collider)
    {
        movementMachine.OnTriggerEnter(collider);
    }

    private void OnTriggerExit(Collider collider)
    {
        movementMachine.OnTriggerExit(collider);
    }

    public void OnMovementStateAnimationEnterEvent()
    {
        movementMachine.OnAnimationEnterEvent();
    }
    
    public void OnMovementStateAnimationExitEvent()
    {
        movementMachine.OnAnimationExitEvent();
    }

    public void OnMovementStateAnimationTransitionEvent()
    {
        movementMachine.OnAnimationTransitionEvent();
    }
}